package com.xckj.cycling.presenter;

import androidx.annotation.NonNull;
import com.xckj.cycling.bean.BaseBean;
import com.xckj.cycling.bean.UserInfoResponseBean;
import com.xckj.cycling.framework.net.RetrofitManager;
import com.xckj.cycling.global.Constant;
import com.xckj.cycling.iview.IRegisterView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import java.util.Objects;

/**
 * @Author: ChenYao
 * @Describe: 注册
 * @Date: 2023/3/5 23:13
 */
public class RegisterPresenter {
    private final IRegisterView registerView;

    public RegisterPresenter(IRegisterView registerView) {
        this.registerView = registerView;
    }

    public void register(String email, String code, String password) {
        registerView.showLoading();
        Call<BaseBean<UserInfoResponseBean>> call = RetrofitManager.getInstance().getService().register(email, code, password);
        call.enqueue(new Callback<BaseBean<UserInfoResponseBean>>() {
            @Override
            public void onResponse(@NonNull Call<BaseBean<UserInfoResponseBean>> call, @NonNull Response<BaseBean<UserInfoResponseBean>> response) {
                if (response.isSuccessful()) {
                    BaseBean<UserInfoResponseBean> baseBean = response.body();
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        registerView.success(baseBean.data.getUserinfo());
                    } else {
                        registerView.failure(baseBean.msg);
                    }
                } else {
                    registerView.failure("login has failed");
                }
                registerView.hideDialog();
            }

            @Override
            public void onFailure(@NonNull Call<BaseBean<UserInfoResponseBean>> call, @NonNull Throwable t) {
                t.printStackTrace();
                registerView.failure("login has failed");
                registerView.hideDialog();
            }
        });
    }
}
